package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.server.pojo.User;
import com.tanhua.server.utils.UserThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: JinboYang
 * @Description:
 * @Date: Created in 下午 18:32 2021/1/11
 * @ModifiedBy:
 */
@Service
@Slf4j
public class QuanziMQService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    /**
     * @Description: 新增动态信息
     * @Param:[publishId]
     * @return:void
     * @Author:JinboYang
     * @Date:2021/1/11
     */
    public void publishMsg(String publishId) {
        this.sendMsg(1, publishId);
    }

    /**
     * @Description: 浏览动态消息
     * @Param:[publishId]
     * @return:void
     * @Author:JinboYang
     * @Date:2021/1/11
     */
    public void queryPublishMsg(String publishId) {
        this.sendMsg(2, publishId);
    }

    /**
     * @Description: 点赞
     * @Param:[publishId]
     * @return:void
     * @Author:JinboYang
     * @Date:2021/1/11
     */
    public void likePublishMsg(String publishId) {
        this.sendMsg(3, publishId);
    }

    /**
     * @Description: 喜欢
     * @Param:[publishId]
     * @return:void
     * @Author:JinboYang
     * @Date:2021/1/11
     */
    public void lovePublishMsg(String publishId) {
        this.sendMsg(4, publishId);
    }

    /**
     * @Description: 评论动态信息
     * @Param:[publishId]
     * @return:void
     * @Author:JinboYang
     * @Date:2021/1/11
     */
    public void commentPublishMsg(String publishId) {
        this.sendMsg(5, publishId);
    }

    /**
     * @Description: 取消点赞
     * @Param:[publishId]
     * @return:void
     * @Author:JinboYang
     * @Date:2021/1/11
     */
    public void disLikePublishMsg(String publishId) {
        this.sendMsg(6, publishId);
    }

    /**
     * @Description: 取消喜欢
     * @Param:[publishId]
     * @return:void
     * @Author:JinboYang
     * @Date:2021/1/11
     */
    public void disLovePublishMsg(String publishId) {
        this.sendMsg(7, publishId);
    }

    /**
     * @Description: 和圈子相关的操作
     * @Param:[type, publishId]
     * @return:void
     * @Author:JinboYang
     * @Date:2021/1/11
     */
    private void sendMsg(Integer type, String publishId) {
        try {
            User user = UserThreadLocal.get();
            Publish publish = this.quanZiApi.queryPublishById(publishId);
            //构建消息对象
            Map<String, Object> msg = new HashMap<>();
            msg.put("pid", publish.getPid());
            msg.put("userId", user.getId());
            msg.put("type", type);
            msg.put("publishId", publishId);
            msg.put("date", System.currentTimeMillis());
            this.rocketMQTemplate.convertAndSend("type", msg);
        } catch (Exception e) {
            log.error("发送消息失败!", e);
        }
    }

}
